package com.tunan.lsj.business.town.web;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.contant.CheckBridge;
import com.github.pagehelper.EasyUiPage;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.tunan.common.Result;
import com.tunan.common.resultSub.ResultFailure;
import com.tunan.common.resultSub.ResultSuccess;
import com.tunan.lsj.business.town.service.JddmService;
import com.tunan.persistence.model.Jddm;
import com.tunan.system.service.IdentifierGenerator;
import com.tunan.system.web.BaseController;
@Controller
@RequestMapping(value="Jddm")
public class JddmController extends BaseController{
	private Logger log = Logger.getLogger(JddmController.class);
	
	@Autowired
	private JddmService jddmService;
	
	@Autowired
	private IdentifierGenerator identifierGenerator;
	
	//校验列
	public static String[] validators = {"jzdmCk"};
	
	/**
	 * 街镇列表
	 */
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public ModelAndView list() {
		return new ModelAndView("/lsj/town/jddm-list");
	}
	
	/**
	 * 查询用户
	 * 
	 * @return
	 */
	@RequestMapping(value = "/search", method = RequestMethod.POST)
	@ResponseBody
	public EasyUiPage search(@ModelAttribute("pageParams") Page pageParams,String wfds) {
		PageHelper.startPage(pageParams.getPageNum(), pageParams.getPageSize());
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("wfds", wfds);
		Page pageresult = (Page) jddmService.selectAll(params);
		return pageresult.getEasyUiPage();
	}
	
	/**
	 * 进入编辑界面
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/showModify", method = RequestMethod.GET)
	public ModelAndView showModify(String id){
		ModelAndView model = new ModelAndView();
		try {
			if(StringUtils.isNotBlank(id)){
				Jddm form = jddmService.selectOne(id);
				model.addObject("vo",form);
			}
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		}
		model.setViewName("/lsj/town/jddm-edit");
		return model;
	}
	
	/**
	 * 保存
	 * @param form
	 * @return
	 */
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	@ResponseBody
	public Result save(@ModelAttribute("form") Jddm form) {
		ResultSuccess rs = new ResultSuccess();
		ResultFailure rf = new ResultFailure();
		try {
			if (form != null) {
				if (StringUtils.isNotBlank(form.getJddm())) {
					jddmService.update(form);
					rs.setMessage("修改成功");
				} else {
					form.setJddm(identifierGenerator.generate());
					CheckBridge.validate(validators,new String[]{form.getJddm()});
					jddmService.insert(form);
					rs.setMessage("新增成功");
				}
			}
			return rs;

		} catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage(), e);
		}
		rf.setMessage("操作失败");
		return rf;
	}
	
	/**
	 * 删除
	 * 
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	@ResponseBody
	public Result delete(String id) {
		try {
			if (StringUtils.isNotBlank(id)) {
				jddmService.delete(id);
			}
			return new ResultSuccess("删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage(), e);
		}
		return new ResultFailure("删除失败");
	}

	/**
	 * 批量删除
	 * 
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "/batchDel", method = RequestMethod.POST)
	@ResponseBody
	public Result batchDel(String ids) {
		try {
			if (StringUtils.isNotBlank(ids)) {
				List<String> idList = new ArrayList<String>();
				if (ids.contains(",")) {
					idList = Arrays.asList(ids.split(","));
				} else {
					idList.add(ids);
				}
				jddmService.batchDel(idList);
			}
			return new ResultSuccess("批量删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage(), e);
		}
		return new ResultFailure("批量删除失败");
	}
}
